package com.robusta.cityuate.sideMenu;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.robusta.cityuate.R;
import com.robusta.cityuate.activities.FavoriteSpotsActivity;
import com.robusta.cityuate.activities.FindSpotsMapActivity;
import com.robusta.cityuate.activities.LoginActivity;
import com.robusta.cityuate.activities.ManageMyspotsActivity;
import com.robusta.cityuate.activities.MyReservationsActivity;
import com.robusta.cityuate.activities.ParkingStatusActivity;
import com.robusta.cityuate.activities.RemindersListActivity;
import com.robusta.cityuate.activities.UserProfileActivity;
import com.robusta.cityuate.utils.Token;

public class SlidingMenuFragment extends ListFragment {

	private Context mContext;
	SideMenuAdapter adapter;

	public interface SlidingMenuControllers{
		public void hideMenu();
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		String[] menuItems = getResources().getStringArray(
				R.array.account_menu_titles);
		TypedArray menuIcons = getResources().obtainTypedArray(
				R.array.account_menu_icons);
		adapter = new SideMenuAdapter(getActivity(), menuItems, menuIcons);
		setListAdapter(adapter);
		adapter.notifyDataSetChanged();
		mContext=getActivity();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent nextActivity=new Intent();
		switch(position){
		case 0:
			nextActivity= new Intent(mContext,FindSpotsMapActivity.class);
			break;
		case 1:
			nextActivity= new Intent(mContext,UserProfileActivity.class);
			break;
		case 2:
			nextActivity=new Intent(mContext,MyReservationsActivity.class);
			break;
		case 3:
			nextActivity=new Intent(mContext,FavoriteSpotsActivity.class);
			break;
		case 4:
			nextActivity= new Intent(mContext,ManageMyspotsActivity.class);
			break;
		case 5:
			nextActivity= new Intent(mContext,ParkingStatusActivity.class);
			break;
		case 6:
			nextActivity=new Intent(mContext,RemindersListActivity.class);
			break;
		case 7:
			Token.removeToken(mContext);
			nextActivity=new Intent(mContext,LoginActivity.class);
			nextActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			break;
		}
		nextActivity.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(nextActivity);
		((SlidingMenuControllers)getActivity()).hideMenu();
		if(position==7)
			getActivity().finish();
		
		
	}

	class SideMenuAdapter extends ArrayAdapter<String> {

		private Context mContext;

		private TypedArray icons;

		public SideMenuAdapter(Context context, String[] menuItems,
				TypedArray menuIcons) {
			super(context, android.R.layout.simple_list_item_1);
			for (String item : menuItems) {
				this.add(item);
			}
			mContext = context;

			this.icons = menuIcons;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			TextView item;
			if (convertView == null)
				convertView = new TextView(mContext);
			item = (TextView) convertView;
			item.setText(getItem(position));
			item.setTextSize(20);
			item.setCompoundDrawablesWithIntrinsicBounds(
					icons.getDrawable(position), null, null, null);
			return item;
		}

	}

}
